<?php
namespace App\Http\Controllers\Agent;
use App\Http\Controllers\AgentController;
use App\Models\Level;
use App\Models\Order;
use App\Models\Set;
use App\Models\Agent;
use Illuminate\Http\Request;
use App\Models\AgentLog;
use App\Models\Recharge;
use App\Models\UpLog;
use App\Models\Service;

class Index extends AgentController{
    public function index(Request $req){
        if (empty($req->agent)) {
            $agent = Agent::where('mid',$req->mem->id)->first();
            if (empty($agent)) wxresp(-1);
            $agent->token = md5(time());
            $agent->save();
            $req->agent = $agent;
        }
        if ($req->post('op') == 'link') wxresp(1,['url'=>"https://".$req->host()."/".$req->agent->code,'poster'=>"https://".$req->host()."/assets/poster.png",'qr'=>['left'=>470,'top'=>1065,'size'=>200]]);
        elseif ($req->post('op') == 'reglink') wxresp(1,['url'=>"https://".$req->host()."/reg/".$req->agent->code]);
        $mem = array();
        $mem['mobile'] = $req->agent->mobile;
        if (empty($req->agent->mid) && $req->mem) Agent::where('id',$req->agent->id)->update(array('mid'=>$req->mem->id));
        $mem['credit'] = round($req->agent->credit,2);
        $mem['level'] = $req->agent->level;
        $mem['code'] = $req->agent->code;
        $mem['leveltitle'] = Level::where('level',$mem['level'])->value('title');
        if ($mem['level'] != Level::max('level')) $mem['canup'] = 1;
        
        $order = [];
        $order['nowith'] = round(Order::where('aid',$req->agent->id)->where('status',1)->sum('reback')+Order::where('paid',$req->agent->id)->where('status',1)->sum('preback'),2);
        $order['canwith'] = round(Order::where('aid',$req->agent->id)->where('status',2)->where('wid',0)->sum('reback')+Order::where('paid',$req->agent->id)->where('status',2)->where('pwid',0)->sum('preback')+UpLog::where('paid',$req->agent->id)->where('wid',0)->sum('credit'),2);
        $order['all'] = round(Order::where('aid',$req->agent->id)->where('status',2)->sum('reback')+Order::where('paid',$req->agent->id)->where('status',2)->sum('preback')+UpLog::where('paid',$req->agent->id)->sum('credit'),2);
        
        $h = date('H:i');
        $url = getImage(Service::where('aid',0)->where("starttime",'<=',$h)->where('endtime','>',$h)->value('url'),1);
        $resp = response()->json(array('code'=>1,'mem'=>$mem,'order'=>$order,'url'=>$url));
        if (isset($agent)) $resp->cookie('superbar_agenttoken',base64_encode($agent->token),365*24*3600);
        return $resp;
    }
    
    public function log(Request $req){
        $page = $req->post('page',1);
        $size = 15;
        $list = AgentLog::where('aid',$req->agent->id)->orderByDesc('id')->offset(($page-1)*$size)->limit($size)->get();
        $datas = [];
        foreach ($list as &$v) {
            if($v->type == 1) $label = Order::where('id',$v->remark)->value('ordersn');
            elseif ($v->type == 2) $label = '在线充值';
            elseif ($v->type == 3) $label = $v->remark ? '升级等级' : '续费等级';
            else $label = $v->remark;
            $datas[] = array('createtime'=>date('Y-m-d H:i',$v->createtime),'price'=>$v->credit,'label'=>$label);
        }
        wxresp(1,['list'=>$datas]);
    }
    
    public function recharge(Request $req){
        $prices = [50,100,200,300,500,1000];
        if ($req->post('op') == 'index') wxresp(1,['credit'=>$req->agent->credit,'items'=>$prices]);
        $index = $req->post('index');
        if (!isset($prices[$index])) wxresp(0,'金额错误');
        $order = array(
                'aid'=>$req->agent->id,
                'ordersn'=>'SR'.date('YmdHis').random(6,1),
                'credit'=>$prices[$index],
                'createtime'=>time()
        );
        $params = array(
                'openid'=>base64_decode($req->cookie('superbar_memlastopenid')),
                'body'=>'余额充值',
                'price'=>$order['credit'],
                'orderno'=>$order['ordersn'],
                'notify_url'=>'https://'.$req->host()."/notify/".$order['ordersn']
        );
        $set = Set::getSet('wxpay');
        $pay = new \WxPay($set);
        $res = $pay->wxappPay($params);
        if($res['code'] == 0) wxresp(0,$res['msg']);
        $orderid = Recharge::insertGetId($order);
        $preresult = $res['paydata'];
        $preresult['orderid'] = $orderid;
        wxresp(1,array('data'=>$preresult));
    }
    
    public function team(Request $req){
        $page = $req->post('page',1);
        $size = 15;
        $list = Agent::where('paid',$req->agent->id)->orderByDesc('id')->offset(($page-1)*$size)->limit($size)->get();
        $datas = [];
        $levels = Level::pluck('title','level');
        foreach ($list as &$v) {
            $datas[] = array('createtime'=>date('Y-m-d H:i',$v->createtime),'level'=>$levels[$v->level],'label'=>$v->mobile);
        }
        wxresp(1,['list'=>$datas]);
    }
}